import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class TestJDBCSelect {
    public static void main(String[] args) throws SQLException {

        DataSource dataSource = new MysqlDataSource();
        ((MysqlDataSource) dataSource).setURL("jdbc:mysql://127.0.0.1:3306/database1?characterEncoding=utf8&useSSL=false");
        ((MysqlDataSource) dataSource).setUser("root");
        ((MysqlDataSource) dataSource).setPassword("root");

        Connection connection = dataSource.getConnection();

        String sql = "select* from student";
        PreparedStatement statement = connection.prepareStatement(sql);

        ResultSet set = statement.executeQuery();// 获取返回集
        // 遍历返回结果
        while (set.next()){
            int id = set.getInt("id");
            String name = set.getString("name");
            System.out.println(id+"   "+name);
        }

        statement.close();
        connection.close();
    }
}
